package com.nf.health.app.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ScreentUtils;

@SuppressLint("DrawAllocation")
public class CircleGradeProgress extends View {

	private int width;
	private float radius;
	private float outLineradius;
	private float inLineradius;
	private float centerX;
	private float centerY;
	private RectF oval;
	private float max = 100;
	private float progress = 0;
	private Paint arcPaint;
	private Paint circlePaint;
	private Paint circleLinePaint;

	private int screenWidth;
	private int bkPaintWidth;
	public CircleGradeProgress(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public CircleGradeProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public CircleGradeProgress(Context context) {
		super(context);
		init(context);
	}
	//24d6da
	//55dfbb
	private void init(Context context) {
		int[] colors = new int[]{Color.parseColor("#24d6da"),Color.parseColor("#55dfbb")};
		bkPaintWidth = DensityUtil.dip2px(context, 20);
		screenWidth = ScreentUtils.getScreenWidth(context);
		
		circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(Color.parseColor("#F0F0F0"));
		circlePaint.setStrokeWidth(bkPaintWidth);
		circlePaint.setStyle(Paint.Style.STROKE);
		
		circleLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circleLinePaint.setColor(Color.parseColor("#E7E7E7"));
		circleLinePaint.setStrokeWidth(2);
		circleLinePaint.setStyle(Paint.Style.STROKE);
		
		LinearGradient shader = new LinearGradient(0, 0, screenWidth/2, screenWidth/2, colors, null,
                Shader.TileMode.MIRROR);
		arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		arcPaint.setColor(Color.parseColor("#24d6da"));
		arcPaint.setStrokeWidth(bkPaintWidth);
		arcPaint.setStrokeCap(Paint.Cap.ROUND);
		arcPaint.setStyle(Paint.Style.STROKE);
		arcPaint.setShader(shader);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
		width = screenWidth / 2;
		centerX = width / 2;
		centerY = centerX;
		radius = centerX - bkPaintWidth/2;
		outLineradius = centerX - 1;
		inLineradius = centerX - bkPaintWidth;
		oval = new RectF(centerX - radius, centerX - radius, centerX + radius,
				centerX + radius);
		setMeasuredDimension(width, width);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(centerX, centerY, radius, circlePaint);
		canvas.drawCircle(centerX, centerY, outLineradius, circleLinePaint);
		canvas.drawCircle(centerX, centerY, inLineradius, circleLinePaint);
		canvas.drawArc(oval, -90, 360 / max * progress, false, arcPaint);
	}

	
	public void setProgress(final float curProgress) {
		if (curProgress > max || curProgress < 0) {
			return;
		}
		new Thread(new Runnable() {

			@Override
			public void run() {
				SystemClock.sleep(500);// 开启Activity的同时会执行动画，跳过这段延迟再进行绘制UI
				for (int i = 0; i <= curProgress; i++) {
					progress++;
					SystemClock.sleep(10);
					postInvalidate();
				}
			}
		}).start();
	}
}
